package com.igeekhome.controller.api;

import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.WaybillRuleDto;
import com.igeekhome.pojo.WWaybillRule;
import com.igeekhome.service.IWaybillRulesService;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author 陈维豪
 */
@RestController
@RequestMapping("/api/waybillRules")
public class WaybillRuleControllerApi {

    @Autowired
    private IWaybillRulesService waybillRulesService;

    @RequestMapping("/list")
    public TableResultBean waybillRuleList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit){
        PageInfo<WaybillRuleDto> pageInfo = waybillRulesService.selectByAll(page, limit);
        Long total = pageInfo.getTotal();
        return new TableResultBean(0,"成功",total,pageInfo.getList());
    }

    @RequestMapping("/delete")
    public ResultBean delete(String idsStr){
        return waybillRulesService.deleteMethod(idsStr);
    }

    @RequestMapping("/add")
    public ResultBean add(WWaybillRule waybillRule){
        waybillRulesService.saveMethod(waybillRule);
        return ResultBean.success();
    }

    @RequestMapping("/update")
    public ResultBean update(WWaybillRule waybillRule){
        waybillRulesService.updateMethod(waybillRule);
        return ResultBean.success();
    }

    @RequestMapping("/changeUseStatus")
    public ResultBean changeUseStatus(Integer id,Integer typeId){
        WWaybillRule wWaybillRule = new WWaybillRule();
        wWaybillRule.setTypeId(typeId);

        wWaybillRule.setId(id);
        wWaybillRule.setUsageStatus("1");
        waybillRulesService.changeUse(wWaybillRule);
        return ResultBean.success();

    }

}
